package com.xjtu.platform.admin.impl;

import com.xjtu.platform.admin.BookService;
import com.xjtu.platform.admin.dao.AddBookDao;
import com.xjtu.platform.admin.dto.AddBookDto;
import com.xjtu.platform.admin.dto.DeleteBookDto;
import com.xjtu.platform.admin.dto.EditBookDto;
import com.xjtu.platform.admin.entity.AllAuthor;
import com.xjtu.platform.admin.entity.AuthorList;
import com.xjtu.platform.common.entity.PrimaryKey;
import com.xjtu.platform.reader.newEntity.LabelAndClass;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: platform
 * @description: 新增图书
 * @author: Wushanxu
 * @create: 2023-11-15 23:29
 **/
@Service
@Transactional
public class BookServiceImpl implements BookService {

    @Resource
    private AddBookDao addBookDao;

    @Override
    public String addBook(AddBookDto addBookDto) {
        try {
            PrimaryKey primaryKey = new PrimaryKey();
            addBookDao.addNewBook(addBookDto, primaryKey);
            String pkId = primaryKey.getId();
            System.out.println("pk:" + primaryKey.getId());
            addBookDao.addNewBookAuthor(addBookDto, pkId);
            addBookDao.addNewBookClass(addBookDto, pkId);
            addBookDao.addNewBookLable(addBookDto, pkId);
            return pkId;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean editBook(EditBookDto editBookDto) {
        try {
            PrimaryKey primaryKey = new PrimaryKey();
            addBookDao.editBook(editBookDto, primaryKey);
            addBookDao.modifyAuthor(editBookDto, primaryKey);
            addBookDao.modifyClass(editBookDto, primaryKey);
            addBookDao.modifyLabel(editBookDto, primaryKey);

            String pkId = primaryKey.getId();
            System.out.println("pk:" + primaryKey.getId());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteBook(DeleteBookDto deleteBookDto) {
        try {
            addBookDao.deleteBookReaderReserveHisByBookId(deleteBookDto);
            addBookDao.deleteReaderBookBorrowHisByBookId(deleteBookDto);
            addBookDao.deleteBookScoreHisByBookId(deleteBookDto);
            addBookDao.deleteBookLableByBookId(deleteBookDto);
            addBookDao.deleteBookPicByBookId(deleteBookDto);
            addBookDao.deleteAuthorBookByBookId(deleteBookDto);
            addBookDao.deleteBookClassByBookId(deleteBookDto);
            addBookDao.deleteBookByBookId(deleteBookDto);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean addBookFile(String bookId, String filename, String fileUrl) {
        try {
            PrimaryKey primaryKey = new PrimaryKey();
            addBookDao.addNewFile(filename, fileUrl, primaryKey);
            String fileId = primaryKey.getId();
            System.out.println("pk:" + primaryKey.getId());
            addBookDao.addNewBookFile(bookId, fileId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean modifyBookFile(String bookId, String filename, String fileUrl) {
        try {
            PrimaryKey primaryKey = new PrimaryKey();
            addBookDao.addNewFile(filename, fileUrl, primaryKey);
            String fileId = primaryKey.getId();
            System.out.println("pk:" + primaryKey.getId());
            addBookDao.deleteBookFile(bookId);
            addBookDao.addNewBookFile(bookId, fileId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public AllAuthor getAuthorList() {
        List<AuthorList> authorList = addBookDao.getAuthorList();
        AllAuthor allAuthor = new AllAuthor();
        allAuthor.setAuthorLists(authorList);
        return allAuthor;
    }


}
